<template>
  <div class="edu-page-warp">
    <condition-card @search="searchList" @reset="reset" label-width="110px">
      <el-row>
        <el-col :span="8">
          <el-form-item :label="$t('label.schoolTerm')">
            <CalendarSelect
              v-model="queryParams.calendarId"
              @change="calendarChange"
              @load-done="loadDone"
            ></CalendarSelect>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="人事所属学院">
            <DictSelect v-model="queryParams.personnelCollege" :data="options.faculty" filterable></DictSelect>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="导师所属学院">
            <DictSelect v-model="queryParams.faculty" :data="options.faculty" filterable></DictSelect>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item :label="$t('label.grade')" v-if="tutorType ==1">
            <!-- <GradeSelect v-model="queryParams.grade" :showAll="false"></GradeSelect> -->
            <el-date-picker
              style="width:100%"
              v-model="queryParams.grade"
              align="right"
              type="year"
              value-format="yyyy"
              placeholder="请选择"
              :clearable="true"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="学堂" v-if="tutorType ==1">
            <DictSelect v-model="queryParams.schoolId" :data="options.school" filterable></DictSelect>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="专业" v-if="tutorType ==2">
            <!-- <MajorSelect :faculty="queryParams.faculty" v-model="queryParams.major"></MajorSelect> -->
            <MyMajor :faculty="queryParams.faculty" v-model="queryParams.major"></MyMajor>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="所属类" v-if="tutorType ==1">
            <SchoolMajorSelect :schoolId="queryParams.schoolId" v-model="queryParams.major"></SchoolMajorSelect>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="是否提交总结">
            <YesNoSelect v-model="queryParams.isSub"></YesNoSelect>
          </el-form-item>
        </el-col>
        <el-col :span="8" v-if="tutorType ==1">
          <el-form-item label="指导次数">
            <el-col :span="11">
              <el-input
                type="number"
                v-model="queryParams.guidanceNumMin"
                controls-position="right"
                :min="0"
                size="mini"
                placeholder="请输入"
                @blur="checkMinElec"/>
            </el-col>
            <el-col :span="2" style="text-align: center;">-</el-col>
                <el-col :span="11">
                  <el-input
                    type="number"
                    v-model="queryParams.guidanceNumMax"
                    controls-position="right"
                    :min="0"
                    size="mini"
                    placeholder="请输入"
                    @blur="checkMaxElec"/>
                </el-col>
          </el-form-item>
        </el-col>
      </el-row>
    </condition-card>
    <el-card shadow="never">
      <div class="edu-page-title">指导记录统计</div>
      <div class="edu-opertion-bar">
        <div class="float-left">
          <!--  <el-col :span="8">
              <DictSelect v-model="queryParams.selectType" :data="options.selectType" :clearable="false" ></DictSelect>
          </el-col>-->
          <KeywordInput v-model="queryParams.condition" @enter="searchList" placeholder="按工号或姓名搜索"></KeywordInput>
        </div>
        <div class="float-right">
          <el-button @click="exportGuidance" type="primary">导出</el-button>
        </div>
      </div>
      <edu-table
        url="/api/welcomeservice/tutorGuidanceRecords/count"
        ref="mainTable"
        :order="false"
        :selection="false"
      >
        <el-table-column label="学期" prop="calendarName" show-overflow-tooltip />
        <el-table-column label="人事所属学院" prop="personnelCollegeI18n" show-overflow-tooltip />
        <el-table-column label="导师所属学院" prop="facultyI18n" show-overflow-tooltip />
        <el-table-column label="专业" prop="majorI18n" v-if="tutorType  == 2" show-overflow-tooltip />
        <el-table-column
          :label="$t('label.grade')"
          prop="grade"
          v-if="tutorType ==1"
          show-overflow-tooltip
        />
        <!-- <el-table-column label="学堂" prop="school" v-if="tutorType  == 1" show-overflow-tooltip />
        <el-table-column label="所属类" prop="majorI18n" v-if="tutorType == 1" show-overflow-tooltip /> -->
        <el-table-column label="导师" prop="teacherName" />
        <el-table-column label="指导次数" prop="guidanceNum" />
        <el-table-column label="指导时长" prop="guidanceCount">
          <template slot-scope="scope">{{scope.row.guidanceCount+'h'}}</template>
        </el-table-column>
        <el-table-column label="是否提交总结" prop="isSub">
          <template slot-scope="scope">{{showSummit(scope.row)}}</template>
        </el-table-column>
      </edu-table>
    </el-card>
  </div>
</template>
<script>
import { queryDic } from "common/src/api/dictionary";
import UTILS from "common/src/utils/utils";
import BPIs from "common/src/api/welcomeService/schoolRelation";
import SchoolMajorSelect from "./SchoolMajorSelect";
import MyMajor from "./MyMajor";
import APIs from "common/src/api/welcomeService/tutorExportImport";

// 指导记录统计
export default {
  name: "GuidanceRecordStatistics",
  props: ["tutorType"],
  components: {
    SchoolMajorSelect,
    MyMajor
  },
  computed: {},
  mounted() {
    this.getOptions();
  },
  watch: {
    "queryParams.grade": {
      handler: function(newVal, oldVal) {
        if (this.queryParams.schoolId) {
          this.queryParams.schoolId = null;
        }
        this.options.school = [];
        if (newVal) {
          this.querySchool.condition.grade = newVal;
          this.findSchool();
        }
      }
    }
  },
  data() {
    return {
      options: {
        faculty: [],
        major: [],
        school: [],
        status: [{ label: "未提交", value: 0 }, { label: "已提交", value: 1 }]
        /*  selectType:[
            {label:'按学生搜索',value:'1'},{label:'按教师搜索',value:'2'}
        ] */
      },
      queryParams: {
        calendarId: null,
        faculty: "",
        major: "",
        isSub: "",
        schoolId: "",
        personnelCollege: "",
        condition: "",
        type: "",
        guidanceNumMin: "",
        guidanceNumMax: ""
      },
      querySchool: {
        pageNum_: 0,
        pageSize_: 0,
        condition: {}
      },
      defaultCalendarId:null
    };
  },
  methods: {
    getOptions() {
      //筛选条件字典查询
      let data = {
        lang: this.$store.getters.language || "cn",
        type: "allChild",
        keys: ["X_YX"]
      };
      queryDic(data).then(res => {
        UTILS.fillSelect(
          {
            faculty: "X_YX"
          },
          res.data,
          this.options
        );
      });
      //this.searchList();
    },
    findSchool() {
      //学堂下拉
      BPIs.findSchool(this.querySchool).then(res => {
        if (res.code == 200 && res.data.list.length > 0) {
          for (const item of res.data.list) {
            this.options.school.push({
              label: item.name,
              value: item.id
            });
          }
        }
      });
    },
    reset() {
      for (const key in this.queryParams) {
        if (key != "calendarId") this.queryParams[key] = "";
      }
      this.queryParams.calendarId=this.defaultCalendarId;
      this.searchList();
    },
    showSummit(data) {
      return data.isSub == 0 ? "否" : "是";
    },
    searchList() {
      this.queryParams.type = this.tutorType;
      this.$refs.mainTable.search(this.queryParams);
    },
    exportGuidance() {
      let loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(255, 255, 255, .48)"
      });
      this.queryParams.type = this.tutorType;
      let data = _.cloneDeep(this.queryParams);
      APIs.exportGuidance(data).then(res => {
        if (res.code == 200) {
          this.delayRequest(res.data.key, loading);
        } else {
          loading.close();
        }
      });
    },
    async delayRequest(key, loading) {
      await new Promise(resolve => setTimeout(resolve, 3000));
      APIs.getReportStatus(key)
        .then(res => {
          if (res.code == 200 && res.data.status) {
            let path = APIs.exportLogUrl + res.data.path;
            //window.open(path);
            window.location.href = path;
            loading.close();
          } else {
            this.delayRequest(key, loading);
          }
        })
        .catch(ex => {
          this.delayRequest(key, loading);
        });
    },
    loadDone() {
      this.defaultCalendarId=this.queryParams.calendarId
      this.searchList();
    },
    checkMinElec() {
      let tempMin = this.queryParams.guidanceNumMin;
      let tempMax = this.queryParams.guidanceNumMax;
      // 如果为空，不用处理
      if (tempMin == '') {
        return;
      }
      // 判断最小值
      if (tempMin < 0) {
        tempMin = 0;
      }
      // 转换为标准格式正数，例如以0开头的数字
      tempMin = Number(tempMin);

      // 如果weekStart大于weekEnd，则weekStart等于weekEnd
      if (tempMax !== '' && tempMin > tempMax) {
        tempMin = tempMax;
      }

      this.queryParams.guidanceNumMin = tempMin;
    },
    checkMaxElec() {
      let tempMin = this.queryParams.guidanceNumMin;
      let tempMax = this.queryParams.guidanceNumMax;
      // 如果为空，不用处理
      if (tempMax === '') {
        return;
      }
      // 判断最小值
      if (tempMax < 0) {
        tempMax = 0;
      }

      // 转换为标准格式正数，例如以0开头的数字
      tempMax = Number(tempMax);

      // 如果weekEnd小于weekStart，则weekEnd等于weekStart
      if (tempMin !== '' && tempMax < tempMin) {
        tempMax = tempMin;
      }

      this.queryParams.guidanceNumMax = tempMax;
    }
  }
};
</script>
<style lang="scss" >
</style>
